<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\company\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use think\facade\Config;
use app\wechat\controller\WechatTool;
/**
 * 企业员工管理
 * Class User
 * @package app\worker\controller
 */
class Viplist extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    // protected $table = 'CompanyUser';
    
	protected $table1 = 'CompanyUserDepartment';
	protected $table_department = 'SystemMenu';
	protected $view_table = 'view_commodity_orders';
	protected $table = 'commodity_orders';
	protected $user = 'user';
    /**
     * 企业员工管理
     * @auth true
     * @menu true
     * @throws \think\Exception
	 
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
		// $where = ['title' => '1', 'is_deleted' => '0'];
        $this->title = 'VIP名单管理';
		
		$dbconf = $this->getconfig('vister_db');
		$res = Db::connect($dbconf)->table('view_store_member')->select();
		$this->fetch('',['list'=>$res]);
		
		var_dump($res);
		exit();
		
		 $create_at_first=trim($this->request->param('create_at_first'));
			 $create_at_last=trim($this->request->param('create_at_last'));
			
			 if(!$create_at_first||!$create_at_last){
			
				
				  $this->_query($this->table)->like('nickname,svn_username,department')->equal('status')->page();
			
				}
				else{
				
				$query = $this->_query('InvitationList')->where('create_at','<= time',$create_at_last)
														->where('create_at','> time',$create_at_first)
														->page();
														
			}
			
				 // $this->_query($this->table)->like('nickname,svn_username,department')->equal('status')->page();
			
		// $data=Db::name('SystemMenu')->where('title="会计部"')->select();
		// $data1=Db::name('CompanyUser')->where('department="会计部"')->select();
		
		// var_dump($data);
		// var_dump($data1);
	
		// if($data=$data1){
		// 	 $this->_query($this->table)->like('nickname,svn_username,department')->equal('status')->page();
		// 	 $list=Db::name('CompanyUser')->where('id')->select();
		// 	
		// }
        // $this->assign('list',$list);
    }

	public function index_search()
	{
		 // $this->_query($this->table)->like('nickname,svn_username,department')->equal('status')->page();
		// $list=Db::name('CompanyUserDepartment')->order('id')->select();
		//   $this->assign('list',$list);
		//   return $this->fetch();
	}
    /**
     * 添加企业员工
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
	
		$data=Db::name('CompanyUser')->where('nickname')->find();
		if(!$data){
		    $dbconf = $this->getconfig('vister_db');
		  $res2= Db::connect($dbconf)->table('view_commodity_orders');
		  //var_dump($res);
		  if($this->request->param('commodityId')){
		  $name= trim($this->request->param('name'));
		  $commodityId=$this->request->param('commodityId');
		  $payTime=$this->request->param('payTime');
		  $expritime=$this->request->param('expritime');
		  $data=[
		      //'name'=> $name,
		      'commodityId'=>$commodityId,
		      'payTime'=>$payTime,
		      'expritime'=>$expritime
		      ];
		      $data_name=['name'=>$name];
		      var_dump($data);
		  
		    $res = Db::connect($dbconf)->table($this->table)->insert($data);
		    $res1= Db::connect($dbconf)->table($this->user)->insert($data_name);
		    $this->success('添加名单成功','');
		  }
			$this->_form($res2, 'form');
		
		}
		else{
				$this->error('名字已存在！');
		}
       
    }


    /**
     * 修改企业员工
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
         	$dbconf = $this->getconfig('vister_db');
         	
         	  if($this->request->param('commodity_title')){
          $id=$this->request->param('id');
		  $name= trim($this->request->param('name'));
		  $commodityId=$this->request->param('commodity_title');
		  $payTime=$this->request->param('payTime');
		  $expritime=$this->request->param('expritime');
		  $data=[
		      //'name'=> $name,
		      //'commodity_title'=>$commodityId,
		      'payTime'=>$payTime,
		      'expritime'=>$expritime
		      ];
		      $data_name=['name'=>$name];
		      $data_title=['title'=>$commodityId];
		      var_dump($data);
		  $where=['id'=>$id];
		    $res = Db::connect($dbconf)->table($this->table)->where($where)->data($data)->update();
		     $res3 = Db::connect($dbconf)->table('commodity')->where($where)->data($data_title)->update();
		   $res1= Db::connect($dbconf)->table($this->user)->where($where)->data($data_name)->update();
		    $this->success('修改名单成功','');
		  }
        $this->_form(Db::connect($dbconf)->table($this->table), 'form');
        // $name = trim($this->request->param('name')); 
        // $commodityId = trim($this->request->param('commodityId')); 
        // var_dump($name);
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            $where = ['status' => '1', 'is_deleted' => '0'];
            $this->auths = Db::name('company_user_auth')->where($where)->order('sort desc,id desc')->select();
            array_unshift($this->auths, ['id' => '0', 'title' => '所有权限', 'path' => '/']);
            $data['svn_authorize'] = isset($data['svn_authorize']) ? explode(',', $data['svn_authorize']) : [];
        } else {
            if (isset($data['svn_authorize']) && is_array($data['svn_authorize'])) {
                $data['svn_authorize'] = join(',', $data['svn_authorize']);
            } else {
                $data['svn_authorize'] = '';
            }
            // $macs = [];
            // foreach (explode(PHP_EOL, preg_replace("/\s+/", PHP_EOL, trim($data['mobile_macs']))) as $mac) {
            //     if (DataService::applyMacValue($mac)) $macs[] = $mac;
            // }
            // $data['mobile_macs'] = join(PHP_EOL, array_unique($macs));
        }
        
    }

    /**
     * 更改企业员工状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $this->_save($this->table, ['status' => input('status', '0')]);
    }

    /**
     * 删除企业员工
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        // $this->_delete($this->table);
		if($this->request->isPost()){      
		$id=trim($this->request->param('id'));
		// dump($id);
		$data=Db::name('CompanyUserDepartment')->where("id in ($id)")->delete();
		if($data){
			$this->success('部门删除成功！');
		}
		}
    }

}
